package com.xiaotu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xiaotu.dto.BuyPropsCardDto;
import com.xiaotu.dto.ConsumePropsCardDto;
import com.xiaotu.mapper.PlayerEquipmentMapper;
import com.xiaotu.model.PlayerEquipmentModel;
import com.xiaotu.service.PlayerEquipmentService;
import com.xiaotu.vo.BuyPropsCardVo;
import com.xiaotu.vo.ConsumePropsCardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
 * @author zhangxiao
 * @create 2022-04-02 10:59
 */
@Service
@Transactional
public class PlayerEquipmentServiceImpl implements PlayerEquipmentService {

    @Autowired
    private PlayerEquipmentMapper playerEquipmentMapper;

    @Override
    public Object consumePropsCard(ConsumePropsCardDto consumePropsCardDto) {
        playerEquipmentMapper.consumePropsCard(consumePropsCardDto);
        ConsumePropsCardVo consumePropsCardVo = new ConsumePropsCardVo();
        BeanUtil.copyProperties(consumePropsCardDto, consumePropsCardVo);
        return consumePropsCardVo;
    }

    @Override
    public BuyPropsCardVo buyPropsCard(BuyPropsCardDto buyPropsCardDto) {
        PlayerEquipmentModel playerEquipmentModel = playerEquipmentMapper.selectByPrimaryKey(buyPropsCardDto.getPlayerId());
        if (Math.subtractExact(buyPropsCardDto.getConsumeGoldCoins(), playerEquipmentModel.getGoldCoins()) > 0)
            throw new IllegalArgumentException("金币不足");
        playerEquipmentMapper.buyPropsCard(buyPropsCardDto);
        BuyPropsCardVo buyPropsCardVo = new BuyPropsCardVo();
        BeanUtil.copyProperties(buyPropsCardDto, buyPropsCardVo);
        return buyPropsCardVo;
    }

    @Override
    public void deductionGoldCoins(List<Integer> playerIds, Integer goldConis) {
        playerEquipmentMapper.deductionGoldCoins(playerIds, goldConis);
    }

    @Override
    public boolean isAdmissionAllowed(Integer playerId) {
        Example example = new Example(PlayerEquipmentModel.class);
        example.createCriteria().andEqualTo("playerId", playerId);
        example.selectProperties("goldCoins");
        if (playerEquipmentMapper.selectOneByExample(example).getGoldCoins().compareTo(100) >= 0)
            return true;
        return false;
    }
}
